﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using NetPE.Core.Metadata;
using NetPE.TreeNodes;

namespace NetPE
{
    class MdRootEditor : BaseEditor
    {
        public class MdStrEditor : BaseEditor
        {
            private DataGridViewTextBoxColumn val;
            private DataGridView view;
            private MdRootNode n;

            public MdStrEditor(MdRootNode n)
            {
                this.n = n;
                view = new DataGridView();
                val = new DataGridViewTextBoxColumn();
                ((ISupportInitialize)view).BeginInit();
                this.SuspendLayout();

                view.BackgroundColor = Color.Black;
                view.BorderStyle = BorderStyle.None;
                view.ColumnHeadersVisible = false;
                view.Columns.Add(val);
                view.Dock = DockStyle.Fill;
                view.ForeColor = Color.Black;
                view.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
                view.RowPostPaint += new DataGridViewRowPostPaintEventHandler(viewRowPostPaint);
                view.VirtualMode = true;
                view.CellValueNeeded += new DataGridViewCellValueEventHandler(viewCellValueNeeded);
                view.CellValuePushed += new DataGridViewCellValueEventHandler(viewCellValuePushed);
                view.NewRowNeeded += new DataGridViewRowEventHandler(viewNewRowNeeded);
                view.UserDeletingRow += new DataGridViewRowCancelEventHandler(viewUserDeletingRow);

                val.HeaderText = "Name";
                val.Name = "name";
                val.Width = 250;

                this.Controls.Add(view);
                ((ISupportInitialize)view).EndInit();
                this.ResumeLayout(false);

            }

            void viewUserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
            {
                (Object as MetadataRoot).RemoveAt(e.Row.Index);
                n.Nodes.RemoveAt(e.Row.Index);
            }

            void viewNewRowNeeded(object sender, DataGridViewRowEventArgs e)
            {
                (Object as MetadataRoot).Add(new MetadataStream(Object as MetadataRoot));
            }

            void viewRowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                e.Graphics.DrawString(e.RowIndex.ToString("x3"), this.Font, Brushes.Black, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - e.Graphics.MeasureString(e.RowIndex.ToString("x3"), this.Font).Height) / 2));
            }

            protected override void RefreshData()
            {
                view.RowCount = (Object as MetadataRoot).Count + 1;
            }

            private void viewCellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
            {
                if (e.RowIndex < (Object as MetadataRoot).Count && e.ColumnIndex == 0)
                {
                    e.Value = (Object as MetadataRoot)[e.RowIndex].Name;
                }
            }

            private void viewCellValuePushed(object sender, DataGridViewCellValueEventArgs e)
            {
                if (e.RowIndex < (Object as MetadataRoot).Count && e.ColumnIndex == 0)
                {
                    (Object as MetadataRoot)[e.RowIndex].Name = (string)e.Value;
                    n.Nodes[e.RowIndex].Text = (string)e.Value;
                }
            }
        }

        protected SplitContainer can;
        private MdStrEditor str;
        private MdRootNode n;

        public MdRootEditor(MdRootNode n)
        {
            this.n = n;
            can = new SplitContainer();
            str = new MdStrEditor(n);
            can.Panel1.SuspendLayout();
            can.Panel2.SuspendLayout();
            can.SuspendLayout();
            this.SuspendLayout();

            can.BackColor = Color.White;
            can.Dock = DockStyle.Fill;
            can.Location = new Point(0, 0);
            can.Orientation = Orientation.Horizontal;
            can.Panel1.Controls.Add(str);
            can.Panel2.BackColor = Color.Black;

            str.Dock = DockStyle.Fill;
            str.Object = this.Object;

            Controls.Add(can);
            can.Panel1.ResumeLayout(false);
            can.Panel2.ResumeLayout(false);
            can.ResumeLayout(false);
            this.ResumeLayout(false);
        }

        protected override void RefreshData()
        {
            this.Controls.Clear();

            can.Panel1.Controls.Clear();
            can.Panel2.Controls.Clear();
            PropertyEditor editor = new PropertyEditor();
            editor.Object = Object;
            editor.Dock = DockStyle.Fill;
            can.Panel1.Controls.Add(editor);
            can.Panel2.Controls.Add(str);

            can.SplitterDistance = 150;
        }
    }
}
